# -*- coding:utf-8 -*-

def index():
#    " 限制选择登录者的模板"
#    try:
#        item_list = db(db.templates.posted_id == auth.user.id).select()
#    except:
#        session.flash = '模板库是空的,需要添加'
#        redirect(URL(r=request))
#    titles = []
#    for item in item_list:
#        titles.append(item.title)
    templates = [i for i in db().select(db.templates.ALL)]
    return dict(
        templates = templates,
        )

@auth.requires_login()
def add():
    form = SQLFORM(db.templates, fields=['title','content','tag'], submit_button='提交')
    if form.accepts(request.vars, session):
        response.flash = '新的模板添加完成'
        redirect(URL(r=request, f='index'))
    return dict(
        form = form
        )
        
@auth.requires_login()
def update():
    form = SQLFORM(db.templates, fields=['title','content','tag'])
    return dict(
        form,
        )

def user(): return dict(form=auth())
